from typing import List, Optional
from datetime import datetime
from pydantic import BaseModel, Field, HttpUrl

from app.schemas.user import UserProfile


class ProjectBase(BaseModel):
    title: str = Field(..., min_length=1, max_length=200)
    description: str = Field(..., min_length=1, max_length=2000)
    repository_url: Optional[HttpUrl] = None
    demo_url: Optional[HttpUrl] = None
    documentation_url: Optional[HttpUrl] = None
    tech_stack: Optional[List[str]] = None
    tags: Optional[List[str]] = None


class ProjectCreate(ProjectBase):
    competition_id: int
    team_id: Optional[int] = None


class ProjectUpdate(BaseModel):
    title: Optional[str] = Field(None, min_length=1, max_length=200)
    description: Optional[str] = Field(None, min_length=1, max_length=2000)
    repository_url: Optional[HttpUrl] = None
    demo_url: Optional[HttpUrl] = None
    documentation_url: Optional[HttpUrl] = None
    tech_stack: Optional[List[str]] = None
    tags: Optional[List[str]] = None
    cover_image: Optional[str] = None
    screenshots: Optional[List[str]] = None


class ProjectList(ProjectBase):
    id: int
    competition_id: int
    team_id: Optional[int]
    created_by: int
    status: str
    score: Optional[float]
    ranking: Optional[int]
    views: int
    cover_image: Optional[str]
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class ProjectResponse(ProjectBase):
    id: int
    competition_id: int
    team_id: Optional[int]
    created_by: int
    status: str
    score: Optional[float]
    ranking: Optional[int]
    views: int
    cover_image: Optional[str]
    screenshots: Optional[List[str]]
    submitted_at: Optional[datetime]
    creator: UserProfile
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True


class ProjectCommentBase(BaseModel):
    content: str = Field(..., min_length=1, max_length=1000)


class ProjectCommentCreate(ProjectCommentBase):
    parent_id: Optional[int] = None


class ProjectCommentResponse(ProjectCommentBase):
    id: int
    project_id: int
    user_id: int
    parent_id: Optional[int]
    user: UserProfile
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True